import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/views/Layout/Layout.vue'
import Home from '../views/Home/Home.vue'
import playVue from '@/views/play.vue'
import useStore from '@/store'
import Message from '@/components/XtxMessage'
const router = createRouter({
    history: createWebHashHistory(),
        scrollBehavior: () => {
            return {
                top: 0,
            }
        },
    routes: [
        {
            path: '/',
            component: Layout,
            children: [
                {
                    path: '',
                    component:Home,
                },
                {
                path: '/category/:id',
                component: () => import('@/views/Category/Category.vue'),
                },
                { path: '/product/:id', component: () => import('@/views/Goods/Goods.vue') },
                {
                    path: '/cart',
                    component: () => import('@/views/cart/cart.vue')
                },
                {
                    path: '/member/checkout',
                    component: () => import('@/views/member/pay/checkout.vue')
                }
            ]
        },
        {
            //动态路由
            path: '/login',
            component:()=>import('@/views/Login/Login.vue')
        },
        {
            path: '/login/callback',
            component: () => import('@/views/Login/Callback.vue')
        },
    ]
})
router.beforeEach((to, from, next) => {
    const { cart } = useStore()
    if (cart.isLogin === false && to.path === '/member/checkout') {
        Message.warning('没有登录不能访问')
        const url = '/login?redirect' + to.fullPath
        next(url)
    } else {
        next()
    }
})
export default router